package com.qf.hospital.mapper;

import com.qf.hospital.entity.Pharmacy;
import com.qf.hospital.entity.PharmacyExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface PharmacyMapper {
    long countByExample(PharmacyExample example);

    int deleteByExample(PharmacyExample example);

    int deleteByPrimaryKey(Integer pharmacyId);

    int insert(Pharmacy record);

    int insertSelective(Pharmacy record);

    List<Pharmacy> selectByExample(PharmacyExample example);

    Pharmacy selectByPrimaryKey(Integer pharmacyId);

    int updateByExampleSelective(@Param("record") Pharmacy record, @Param("example") PharmacyExample example);

    int updateByExample(@Param("record") Pharmacy record, @Param("example") PharmacyExample example);

    int updateByPrimaryKeySelective(Pharmacy record);

    int updateByPrimaryKey(Pharmacy record);

    /**
     * 根据条件查询药品
     * @param drugname
     * @return
     */
    List<Pharmacy> findPharmacyByDrugName(@Param("drugname") String drugname);

    /**
     * 查询当前药品的库存数量，（可以开启悲观锁，但是主要索引是否生效问题）
     * @param pharmacyName
     * @return
     */
    @Select("select * from pharmacy WHERE pharmacy_name=#{pharmacyName} FOR UPDATE")
    Pharmacy findPharmacyName(@Param("pharmacyName") String pharmacyName);

    /**
     * 修改药品库存
     * @param pharmacyName
     * @param pharmacyNum
     */
    @Update("update pharmacy set drug_store_num=drug_store_num+#{pharmacyNum} where pharmacy_name=#{pharmacyName}")
    Integer addInventoryByPharmacyName(@Param("pharmacyName") String pharmacyName, @Param("pharmacyNum") Integer pharmacyNum);
}